package com.csii.result;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 统一返回结果类
 */
@Data
@Accessors(chain = true)
public class APIResult<T> {
    private APIResult(){}

    /**
     * 状态码 成功为200 其他都是失败
     */
    private String code;

    /**
     * 成功为success，其他为失败原因
     */
    private String message;

    /**
     * 数据结果集
     */
    private T data;

    private String tableHtml;

    public static <T> APIResult<T> successResult(){
        APIResult<T> apiResult = new APIResult<T>();
        apiResult.setCode(ResultCode.SUCCESS.code);
        apiResult.setMessage(ResultCode.SUCCESS.message);
        return apiResult;
    }

    public static <T> APIResult<T> successResult(T data){
        APIResult<T> apiResult = new APIResult<T>();
        apiResult.setCode(ResultCode.SUCCESS.code);
        apiResult.setMessage(ResultCode.SUCCESS.message);
        apiResult.setData(data);
        return apiResult;
    }

    public static <T> APIResult<T> failResult(T data,String message){
        APIResult<T> apiResult = new APIResult<T>();
        apiResult.setCode(ResultCode.FAILED.code);
        apiResult.setMessage(message);
        apiResult.setData(data);
        return apiResult;
    }
}
